<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">名称：</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="name" class="layui-input" type="text" placeholder="请输入名称"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">编码：</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="code" class="layui-input" type="text" placeholder="请输入编码"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">出入库性质：</label>
                                    <div class="layui-input-inline mr0">
                                        <select name="inventoryType">
                                            <option value="">请选择出入库性质</option>
                                            <option value="in">入库</option>
                                            <option value="out">出库</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">状态：</label>
                                    <div class="layui-input-inline mr0">
                                        <select name="status">
                                            <option value="">请选择状态</option>
                                            <option value="enable">启用</option>
                                            <option value="unable">停用</option>
                                        </select>
                                    </div>
                                </div>
                                <!-- <div class="layui-inline">
                                    <label class="layui-form-label w-auto">支持退换：</label>
                                    <div class="layui-input-inline mr0">
                                        <select name="supportReturn">
                                            <option value="">请选择状态</option>
                                            <option value="1">支持</option>
                                            <option value="0">不支持</option>
                                        </select>
                                    </div>
                                </div> -->
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit>查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                            <div class="query-rt"></div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="warehouseInventoryTypeBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
                        </div>
                        <div class="lay-btn-rt"></div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="warehouseInventoryTypeTable" lay-filter="warehouseInventoryTypeTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="warehouseInventoryTypeTableBar">
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/html" id="statusEnum">
    <!-- {{#  if(d.status == '停用'){}} -->
        <span>停用</span>
    <!-- {{#}else if(d.status == '启用'){}} -->
        <span>启用</span>
    <!-- {{#}}} -->
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="warehouseInventoryTypeForm">
    <form lay-filter="warehouseInventoryTypeForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入名称" type="text" class="layui-input" lay-verify="required|isOnlyName" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">编码</label>
            <div class="layui-input-block">
                <input name="code" placeholder="请输入编码" type="text" class="layui-input" lay-verify="required|isOnlySn" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">操作类型</label>
            <div class="layui-input-block">
                <select name="operateType">
                    <option value="purchase">采购</option>
                    <option value="produce">生产</option>
                    <option value="transfer">调拨</option>
                    <option value="inventory">盘点</option>
                    <option value="market">销售</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">出入库性质</label>
            <div class="layui-input-block">
                <select name="inventoryType">
                    <option value="in">入库</option>
                    <option value="out">出库</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">支持退换</label>
            <div class="layui-input-block">
                <select name="supportReturn">
                    <option value="1">支持</option>
                    <option value="0">不支持</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <select name="status">
                        <option value="enable">启用</option>
                        <option value="unable">停用</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">序号</label>
            <div class="layui-input-block">
                <input name="sortNumber" placeholder="请输入序号" type="text" class="layui-input" lay-verify="sortNumber" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注描述</label>
            <div class="layui-input-block">
                <textarea name="description" placeholder="请输入备注" maxlength="200" class="layui-textarea" lay-verify="" required></textarea>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="warehouseInventoryTypeFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;

        // 渲染表格
        var insTb = table.render({
            elem: '#warehouseInventoryTypeTable',
            url: config.base+ 'enterprisecab/warehouse/admin/warehouseInventoryType/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers'}
                ,{field: 'code', align:'center', title: '编码', width: 180}
                ,{field: 'name', align:'center', title: '名称', width: 180}
                ,{field: 'operateTypeDesc', align:'center', title: '操作类型'}
                ,{field: 'inventoryTypeDesc', align:'center', title: '出入库性质'}
                ,{field: 'supportReturn', align:'center', title: '支持退换',templet:function(d){
                    return d.supportReturn == 1 ? '支持' : '不支持';
                }   
        }
                ,{field: 'statusDesc', align:'center', title: '状态'}
                ,{field: 'sortNumber', align:'center', title: '排序'}
                ,{field: 'description', align:'center', title: '备注'}
                ,{field: 'createTime', align:'center', title: '创建时间', width: 160}
                ,{align: 'center', toolbar: '#warehouseInventoryTypeTableBar', title: '操作', width: 200}
            ]]
        });

        // 工具条点击事件
        table.on('tool(warehouseInventoryTypeTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                data.eventType='edit';
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(warehouseInventoryTypeTable)', function(obj){
            table.reload('warehouseInventoryTypeTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        form.verify({
            isOnlySn: function (value) {
                var isOnly = true;
                var idVal=$("form input[name='id']").val();
                admin.req('enterprisecab/warehouse/admin/warehouseInventoryType/isOnly', {code:value,id:idVal}, function (res) {
                    if (res.code == 200) {
                        isOnly = res.data;
                    }
                }, 'GET',false);
                if(!isOnly){
                    return "出入库性质编码必须唯一"
                }
            },
            isOnlyName: function (value) {
                var isOnly = true;
                var idVal=$("form input[name='id']").val();
                var nameVal=$("form input[name='name']").val();
                admin.req('enterprisecab/warehouse/admin/warehouseInventoryType/isOnly', {name:nameVal,id:idVal}, function (res) {
                    if (res.code == 200) {
                        isOnly = res.data;
                    }
                }, 'GET',false);
                if(!isOnly){
                    return "出入库性质名称必须唯一"
                }
            }
        });

        //渲染下拉框
        form.render('select');

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            let field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
        });

        // 添加按钮点击事件
        $('#warehouseInventoryTypeBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '640px',
                offset: '120px',
                title: data ? '修改出入库类型' : '添加出入库类型',
                content: $('#warehouseInventoryTypeForm').html(),
                success: function () {
                    if (data){
                        var option = $("form select[name='status']").find("option");
                        layui.each(option, function(index, item){
                            if(data.status  == item.text){
                                data.status = item.value;
                            }
                        });
                    }
                    form.val('warehouseInventoryTypeForm', data);
                    // 表单提交事件
                    form.on('submit(warehouseInventoryTypeFormSubmit)', function (d) {
                        layer.load(2);
                        var url='enterprisecab/warehouse/admin/warehouseInventoryType/add';
                        if(data){
                            url='enterprisecab/warehouse/admin/warehouseInventoryType/update';
                        }
                        admin.req(url, d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('warehouseInventoryTypeTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '120px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprisecab/warehouse/admin/warehouseInventoryType/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#madePlanTypeBtnExport').click(function () {
            var checkRows = table.checkStatus('warehouseInventoryTypeTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>